package cn.glutnn.auth.application.controller;

import cn.glutnn.auth.application.convert.AuthRolePermissionConverter;
import cn.glutnn.auth.application.dto.AuthRolePermissionDTO;
import cn.glutnn.auth.application.entity.Result;
import cn.glutnn.auth.domain.entity.AuthRolePermissionBO;
import cn.glutnn.auth.domain.service.AuthRolePermissionDomainService;
import com.google.common.base.Preconditions;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Author:   msh
 * Date:     2025/1/14 下午3:27
 * Description:
 */

@RestController
@RequestMapping("/rolePermission")
@Slf4j
public class RolePermissionController {
    @Resource
    private AuthRolePermissionDomainService authRolePermissionDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AuthRolePermissionDTO authRolePermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthRolePermissionController add authPermissionDTO:{}", authRolePermissionDTO);
            }
            Preconditions.checkNotNull(authRolePermissionDTO.getRoleId(), "角色id不能为空");
//            Preconditions.checkNotNull(authRolePermissionDTO.getPermissionIds(), "权限id不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(authRolePermissionDTO.getPermissionIds()), "权限关联不能为空");
            AuthRolePermissionBO authRolePermissionBO = AuthRolePermissionConverter.INSTANCE.convertDTOToBO(authRolePermissionDTO);
            return Result.ok(authRolePermissionDomainService.add(authRolePermissionBO));
        }catch (Exception e){
            log.error("AuthRolePermissionController add error:{}", e.getMessage(), e);
            return Result.fail("添加失败");
        }
    }
}
